package com.zry.pda.presenter

import android.annotation.SuppressLint
import android.content.Intent
import android.util.Log
import android.view.View
import com.zry.pda.R
import com.zry.pda.adapter.CodeListAdapter
import com.zry.pda.animation.RecyclerViewUtilKt
import com.zry.pda.been.*
import com.zry.pda.greendao.entity.FlowRecordInfo
import com.zry.pda.retrofit.ShowUserView
import com.zry.pda.ui.activity.OperationRecordInfoActivity
import com.zry.pda.ui.activity.print.GenerateRqCodeActivity
import com.zry.pda.utils.StringListUtil
import kotlinx.android.synthetic.main.activity_operation_record_info.*

class OperationRecordInfoPresenter(private val activity: OperationRecordInfoActivity) : ShowUserView {
    private var recyclerViewUtilKt: RecyclerViewUtilKt? = null
    private var httpResponseData: HttpResponse<FlowCodeDetail>? = null
    fun initRecycler() {
        recyclerViewUtilKt = RecyclerViewUtilKt(activity, activity.operationRecordInfoRecycler)
        recyclerViewUtilKt?.initRecyclerView()
    }

    private fun initAdapter(codeList: MutableList<CodeList>) {
        val codeListAdapter = CodeListAdapter(R.layout.code_list_item, codeList)
        recyclerViewUtilKt?.setAdapter(codeListAdapter)
    }


    fun getFlowCodeDetailList() {
        when (activity.title) {
            "批量装箱", "号段装箱", "打包记录" -> {
                HttpServer(this).getFlowCodeDetailList(activity.flowCodeID, activity)

                activity.operationRecordInfoLinear.visibility = View.VISIBLE
                activity.operationRecordInfoBtn.text = "打印二维码"
            }
            "入库记录" -> {
                HttpServer(this).getStoreDetailInfo(activity.detailID, activity)
            }
            "出库记录" -> {
                HttpServer(this).getOutDetailInfo(activity.detailID, activity)
            }
            "补码记录", "拆码记录" -> {
                HttpServer(this).getFlowRecordInfo(activity.recordID, activity)
            }
        }

        activity.operationRecordInfoBtn.setOnClickListener {
            val intent = Intent(activity, GenerateRqCodeActivity::class.java)
            intent.putExtra("productName", httpResponseData!!.ResultData.ProductName)
            intent.putExtra("DataList", httpResponseData!!.ResultData.DataList)
            activity.startActivity(intent)
        }
    }

    @SuppressLint("SetTextI18n")
    override fun toMainActivity(what: Int, data: Any) {
        when (what) {
            9 -> {
                httpResponseData = data as HttpResponse<FlowCodeDetail>
                Log.e("TAG", data.toString())
                if (httpResponseData!!.State == "1") {
                    activity.infoAcceptDealer.text = "产品名称"
                    activity.infoAcceptDealerTV.text = httpResponseData!!.ResultData.ProductName
                    activity.infoProductSpec.text = "产品规格"
                    activity.infoProductSpecTV.text = httpResponseData!!.ResultData.NetUnitPack
                    activity.infoProductionBatch.text = "装箱比例"
                    activity.infoProductionBatchTV.text = httpResponseData!!.ResultData.Packing + ":1"
                    activity.infoProductionLine.text = "包含数量"
                    activity.infoProductionLineTV.text = httpResponseData!!.ResultData.DetailNum
                    activity.infoPutStorageMethodLinear.visibility = View.GONE
                    setCodeList(
                        httpResponseData!!.ResultData.CodeS,
                        httpResponseData!!.ResultData.MaxCode,
                        httpResponseData!!.ResultData.MinCode
                    )
                }
            }
            15 -> {
                val resultData = data as StoreDetail
                if (resultData.state == "1") {
                    activity.infoAcceptDealer.text = "产品名称"
                    activity.infoAcceptDealerTV.text = resultData.ProductName
                    activity.infoProductSpecTV.text = resultData.NetUnitPack
                    activity.infoProductionBatchTV.text = resultData.BatchName
                    activity.infoProductionLineTV.text = resultData.ProductionLineName
                    when (resultData.StoreType) {
                        "1" -> {
                            activity.infoPutStorageMethodTV.text = "单码入库"
                        }
                        "2" -> {
                            activity.infoPutStorageMethodTV.text = "二级码入库"
                        }
                        "3" -> {
                            activity.infoPutStorageMethodTV.text = "三级码入库"
                        }
                    }
                    setCodeList(resultData.CodeS, resultData.MaxCode, resultData.MinCode)
                }
            }
            20 -> {
                val resultData = data as OutDetailBean
                if (resultData.state == "1") {
                    activity.infoAcceptDealer.text = "产品名称"
                    activity.infoAcceptDealerTV.text = resultData.ProductName
                    activity.infoProductSpecTV.text = resultData.NetUnitPack
                    activity.infoProductionBatch.text = "接收经销商"
                    activity.infoProductionBatchTV.text = resultData.CustomerName
                    activity.infoProductionLine.text = "出库方式"

                    when (resultData.OutStockPackLeval) {
                        "1" -> activity.infoProductionLineTV.text = "单码出库"
                        "2" -> activity.infoProductionLineTV.text = "二级码出库"
                        "3" -> activity.infoProductionLineTV.text = "三级码出库"
                    }
                    activity.infoPutStorageMethodLinear.visibility = View.GONE


                    if (resultData.OutStockType=="3"){

                    }else{
                        setCodeList(resultData.CodeS, resultData.MaxCode, resultData.MinCode)
                    }
                }

            }
            25 -> {
                val resultData = data as FlowRecordInfo
                if (resultData.state == "1") {
                    activity.infoAcceptDealer.text = "父级码"
                    activity.infoProductSpec.text = "产品名称"
                    activity.infoProductionBatch.text = "产品规格"
                    activity.infoProductionLine.text = "装箱比例"
                    activity.infoPutStorageMethodLinear.visibility = View.GONE


                    activity.infoAcceptDealerTV.text = resultData.codeID
                    activity.infoProductSpecTV.text = resultData.productName
                    activity.infoProductionBatchTV.text = resultData.netUnitPack
                    activity.infoProductionLineTV.text = resultData.recordType + ":1"
                    val codeList: MutableList<String> = StringListUtil().getList(resultData.codeS)
                    val codeResultList: MutableList<CodeList>? = mutableListOf()
                    codeList.forEachIndexed { index, it ->
                        codeResultList?.add(index, CodeList(it))
                    }
                    activity.codeListNumberTV.text = "追溯码列表(共${codeResultList!!.size}条)"
                    initAdapter(codeResultList)
                }
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun setCodeList(CodeS: String, MaxCode: String, MinCode: String) {
        if (CodeS != "" && CodeS != null) {
            val codeList: MutableList<String> = StringListUtil().getList(CodeS)
            val codeResultList: MutableList<CodeList>? = mutableListOf()
            codeList.forEachIndexed { index, it ->
                codeResultList?.add(index, CodeList(it))
            }
            activity.codeListNumberTV.text = "追溯码列表(共${codeResultList!!.size}条)"
            initAdapter(codeResultList)
        } else {
            val codeStr = (StringListUtil().getStringIntercept(MaxCode, 6).toInt()
                    - StringListUtil().getStringIntercept(MinCode, 6).toInt()) + 1
            activity.codeListNumberTV.text = "追溯码列表(共${codeStr}条)"
            activity.operationRecordLinear.visibility = View.VISIBLE
            activity.operationRecordInfoRecycler.visibility = View.GONE
            activity.optionRecordStartCode.text = MinCode
            activity.optionRecordEndCode.text = MaxCode
        }
    }
}